<template>
<div>
  <div class="crumbs">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>
        <i class="el-icon-menu"></i> 电影管理
      </el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <div class="container">
    <div class="handle-box">
      <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="delAllSelection"
      >批量删除</el-button>
      <el-select v-model="query. movieType" placeholder="电影类型" class="handle-select mr10">
        <el-option key="1" label="惊悚" value="惊悚"></el-option>
        <el-option key="2" label="犯罪" value="犯罪"></el-option>
      </el-select>
      <el-input v-model="query.movieName" placeholder="电影名" class="handle-input mr10"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <el-button type="primary" icon="el-icon-circle-plus-outline" @click="centerDialogVisible=true">添加</el-button>
    </div>
    <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column prop="movId" label="id" width="55" align="center"></el-table-column>
      <el-table-column prop="movName" label="电影名"></el-table-column>
      <el-table-column prop="movNameEn" label="电影英文名"></el-table-column>
      <el-table-column label="海报(查看大图)" align="center">
        <template slot-scope="scope">
          <el-image
              style="width: 100px; height: 100px"
              v-if="scope.row.movImgurl"
              :src="scope.row.movImgurl"
              :preview-src-list="[scope.row.movImgurl]"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="movTypeId" label="电影类型"></el-table-column>
      <el-table-column prop="movAreaId" label="电影区域"></el-table-column>
      <el-table-column prop=" movReleaseTime" label="电影放映时间">
        <template slot-scope="scope">
         <span>{{opertingTime(scope.row.movReleaseTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="movDuration" label="电影时长"></el-table-column>
      <el-table-column prop="movReleaseAreaId" label="电影上映区域"></el-table-column>
      <el-table-column prop="movWantSee" label="电影想看数"></el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button
              type="text"
              icon="el-icon-edit"
              @click=""
          >编辑</el-button>
          <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click=""
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
      ></el-pagination>
    </div>
  </div>
<!--添加电影弹出框-->
  <div >
    <el-dialog
        title="添加电影"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>
      <el-form :model="addMovieForm" :rules="rules"  ref="addMovieForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="电影中文名" prop="movName">
          <el-input v-model="addMovieForm.movName"></el-input>
        </el-form-item>
        <el-form-item label="电影英文名" prop="movNameEn">
          <el-input v-model="addMovieForm.movNameEn"></el-input>
        </el-form-item>
        <el-form-item label="电影类型" prop="movTypeId">
          <el-input v-model="addMovieForm.movTypeId"></el-input>
        </el-form-item>
        <el-form-item label="电影区域" prop="movAreaId">
          <el-select v-model="addMovieForm.movAreaId">
            <el-option label="中国大陆" value="1" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电影放映时间">
            <el-form-item prop="movReleaseTime">
              <el-date-picker type="date" placeholder="选择日期" v-model="addMovieForm.movReleaseTime" style="width: 70%;"></el-date-picker>
            </el-form-item>
        </el-form-item>
        <el-form-item label="电影时长" prop="movDuration">
          <el-input v-model="addMovieForm.movDuration"></el-input>
        </el-form-item>
        <el-form-item label="电影上映区域" prop="movReleaseAreaId">
          <el-select v-model="addMovieForm.movReleaseAreaId">
            <el-option label="横店影城" value="1" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电影简介" prop="movSummary">
          <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入电影简介"
              v-model="addMovieForm.movSummary">
          </el-input>
        </el-form-item>
        <el-form-item label="海报" prop="movImgurl">
          <el-upload
              class="upload-demo"
              drag
              action="https://jsonplaceholder.typicode.com/posts/"
              multiple
              v-model="addMovieForm.movImgurl">>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="centerDialogVisible = false">保存</el-button>
  </span>
    </el-dialog>
  </div>



</div>
</template>

<script>
import store from "../../store";

export default {
  name: "Movies",
  data() {
    return {
      query: {
        movieType: '',
        movieName: '',
        pageIndex: 1,
        pageSize: 10
      },
      tableData: [],
      delList: [],
      pageTotal: 0,
      form: {},
      idx: -1,
      id: -1,
      centerDialogVisible:false,//添加电影弹出框,为false不弹出
      addMovieForm:{
        movId:'',//电影ID
        movName:'',//电影中文名
        movNameEn:'',//电影英文名
        movTypeId:'',//电影类型
        movAreaId:'',//电影区域ID
        movReleaseTime:'',//电影放映时间
        movDuration:'',//电影时长
        movReleaseAreaId:'',//电影上映区域ID
        movSummary:'',//电影简介
        movWantSee:0,//电影想看数,默认值0
        movImgurl:''
      },//添加电影form表单
      rules:{
        movName: [
            { required: true, message: '请输入电影中文名', trigger: 'blur' },
          ],
        movNameEn: [
          { required: true, message: '请输入电影英文名', trigger: 'blur' },
        ],
        movTypeId: [
          { required: true, message: '请输入电影类型', trigger: 'blur' },
        ],
        movReleaseTime: [
          { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
        ],
        movDuration: [
          { required: true, message: '请输入电影时长', trigger: 'blur' },
        ],
        movSummary:[
          { required: true, message: '请填写电影简介', trigger: 'blur' }
        ]
        }
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 获取 数据库 的数据
    getData() {
      this.$axios.get("/movie").then(res => {
        console.log(res.data);
        this.tableData = res.data.data;
      });
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, 'pageIndex', 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      })
          .then(() => {
            this.$message.success('删除成功');
            this.tableData.splice(index, 1);
          })
          .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      const length = this.multipleSelection.length;
      let str = '';
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
      }
      this.$message.error(`删除了${str}`);
      this.multipleSelection = [];
    },
    // 编辑操作
    handleEdit(index, row) {
      this.idx = index;
      this.form = row;
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit() {
      this.editVisible = false;
      this.$message.success(`修改第 ${this.idx + 1} 行成功`);
      this.$set(this.tableData, this.idx, this.form);
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, 'pageIndex', val);
      this.getData();
    },
    //操作时间的时间戳
    opertingTime(timeDate){
      return this.$moment(timeDate).format('yyyy-MM-DD')
    }
  }
};

</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>